package org.myorg.datastraemapi.demo;


import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.myorg.datastraemapi.model.Student;

import java.util.ArrayList;
import java.util.List;

// 未完
public class WindowDemo {
	public static void main(String[] args) throws Exception {
		final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

		List<Student> listSource = new ArrayList<Student>() {{
			add(new Student(1, 1));
			add(new Student(2, 2));
			add(new Student(3, 3));
			add(new Student(4, 4));
			add(new Student(5, 4));
			add(new Student(6, 4));
			add(new Student(7, 4));
		}};
		DataStreamSource<Student> dataStreamSource = env.fromCollection(listSource);


		KeyedStream<Student, Integer> keyBy = dataStreamSource.keyBy(new KeySelector<Student, Integer>() {
			@Override
			public Integer getKey(Student student) throws Exception {
				return student.age;
			}
		});

		WindowedStream window = keyBy.window(TumblingEventTimeWindows.of(Time.milliseconds(1)));
		SingleOutputStreamOperator<Student> maxBy = window.maxBy("age");
		maxBy.print();

		env.execute("Flink Streaming Java API Skeleton");
	}
}
